package com.ibm.cloud.banking.generator;

import org.slf4j.MDC;

import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

public class SleuthIdGenerator implements ITransIdGenerator {

	static final String MDC_TRACE_ID="traceId";

	ThreadLocalRandom rand=ThreadLocalRandom.current();

	@Override
	public String nextId() {
		return Optional.ofNullable(MDC.get(MDC_TRACE_ID)).orElseGet(
				()->{ String traceId= new UUID(rand.nextInt(),rand.nextInt()).toString();
				MDC.put(MDC_TRACE_ID, traceId);
				return traceId;});
	}

}
